package com.goktech.olala.client.controller.backstage;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.goktech.olala.client.controller.basic.BasicController;
import com.goktech.olala.core.config.SysConfig;
import com.goktech.olala.core.service.ICommentService;
import com.goktech.olala.server.pojo.comment.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/commentApi")
public class CommentController extends BasicController {

    @Autowired
    ICommentService commentService;

    /*查询所有评论*/
    @RequestMapping(value = "/queryComment.do")
    @ResponseBody
    public ModelMap queryComment(HttpServletRequest request, Integer sEcho,
                                 Integer iDisplayStart, Integer iDisplayLength) {
        ModelMap resultMap = new ModelMap();
        Integer respCode = SysConfig.SUCCESS_CODE;
        String respMsg = "";
        JSONObject respData = new JSONObject();
        try {
            JSONObject params = super.reqParamsFormat(request);
            Comment comment = build(params);
            int pageIndex = iDisplayStart / iDisplayLength + 1;
            PageInfo<Comment> pageInfo = commentService.queryAllComment(pageIndex, iDisplayLength, comment);
            for (Comment comment1 : pageInfo.getList()) {
                System.out.println(comment1);
            }
            if (pageInfo != null && CollectionUtils.isEmpty(pageInfo.getList())) {
                respCode = -10000;
                respMsg = "查询结果集为空!";
            }
            respData = super.respParamsFormat(respCode, respMsg, sEcho, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            respCode = -10001;
            respMsg = e.getMessage();
            respData = super.respParamsFormat(respCode, respMsg, null);
        } finally {
            resultMap.put("respData", respData);
            return resultMap;
        }
    }

    /*意见反馈*/
    @RequestMapping(value = "/queryAdvices.do")
    @ResponseBody
    public ModelMap queryAdvice(HttpServletRequest request, Integer sEcho,
                                 Integer iDisplayStart, Integer iDisplayLength) {
        ModelMap resultMap = new ModelMap();
        Integer respCode = SysConfig.SUCCESS_CODE;
        String respMsg = "";
        JSONObject respData = new JSONObject();
        try {
            JSONObject params = super.reqParamsFormat(request);
            Comment comment = build(params);
            int pageIndex = iDisplayStart / iDisplayLength + 1;
            PageInfo<Comment> pageInfo = commentService.queryAdvices(pageIndex, iDisplayLength, comment);
            for (Comment comment1 : pageInfo.getList()) {
                System.out.println(comment1);
            }
            if (pageInfo != null && CollectionUtils.isEmpty(pageInfo.getList())) {
                respCode = -10000;
                respMsg = "查询结果集为空!";
            }
            respData = super.respParamsFormat(respCode, respMsg, sEcho, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            respCode = -10001;
            respMsg = e.getMessage();
            respData = super.respParamsFormat(respCode, respMsg, null);
        } finally {
            resultMap.put("respData", respData);
            return resultMap;
        }
    }
    /*删除评论*/
    @RequestMapping(value = "/deleteComment.do")
    @ResponseBody
    public ModelMap deleteComment(HttpServletRequest request){
        ModelMap resultMap = new ModelMap();
        Integer respCode = SysConfig.SUCCESS_CODE;
        String respMsg = "删除成功!";
        JSONObject respData = new JSONObject();
        try {
            JSONObject params = super.reqParamsFormat(request);
            Long commentId = params.getLong("commentId");


            if (commentId == null) {
                respCode = -10002;
                respMsg = "commentId输入参数错误！";

                respData = super.respParamsFormat(respCode, respMsg, null);
                resultMap.put("respData", respData);
                return resultMap;
            }
            int result = commentService.removeCommentById(commentId);
            respData = super.respParamsFormat(respCode, respMsg, result);
        } catch (Exception e) {
            e.printStackTrace();
            respCode = -10001;
            respMsg = e.getMessage();
            respData = super.respParamsFormat(respCode, respMsg, null);
        } finally {
            resultMap.put("respData", respData);
            return resultMap;
        }
    }

    /*=====================================查询实体类=====================================*/
    public Comment build(JSONObject params) {
        Comment comment = new Comment();
        Long commentId = params.getLong("commentId");
        Integer commentType = params.getInteger("commentType");
        String idValue = params.getString("idValue");
        Long orderId = params.getLong("orderId");
        String userId = params.getString("userId");
        String userName = params.getString("userName");
        String email = params.getString("email");
        String title = params.getString("title");
        Integer commentRank = params.getInteger("commentRank");
        String addTime = params.getString("addTime");
        String ipAddr = params.getString("ipAddr");
        Integer parentId = params.getInteger("parentId");
        Integer auditStatus = params.getInteger("auditStatus");
        String auditTime = params.getString("auditTime");
        String modifiedTime = params.getString("modifiedTime");
        String content = params.getString("content");

        comment.setCommentId(commentId);
        comment.setCommentType(commentType);
        comment.setIdValue(idValue);
        comment.setOrderId(orderId);
        comment.setUserId(userId);
        comment.setUserName(userName);
        comment.setEmail(email);
        comment.setTitle(title);
        comment.setCommentRank(commentRank);
        comment.setAddTime(addTime);
        comment.setIpAddr(ipAddr);
        comment.setParentId(parentId);
        comment.setAuditStatus(auditStatus);
        comment.setAuditTime(auditTime);
        comment.setModifiedTime(modifiedTime);
        comment.setContent(content);
        return comment;
    }
}